local _M = {}

local tracer = require("skywalking.tracer")

function _M.doWithLocalSpan(func, operationName, ...)
    local local_span = tracer:createLocalSpan(ngx.ctx, operationName or "function", "function")
    local_span:setComponentId(local_span.ComponentId_Constants.LuaComponentId)
    local_span:setLayer(local_span.SpanLayer.NONE)
    local ok,ret,err = pcall(func, ...)
    if not ok then
        local_span:error(ngx.now() * 1000, { key = "err1", value = tostring(ret)})
        local_span:finish(ngx.now() * 1000)
        return nil, tostring(ret)
    end

    local_span:finish(ngx.now() * 1000)
    return ret, err
end

return _M
